package problem1218;

import java.util.HashMap;
import java.util.Map;

//1218.最长定差子序列
//https://leetcode.cn/problems/longest-arithmetic-subsequence-of-given-difference/

class Solution {
    public int longestSubsequence(int[] arr, int difference) {
        //创建一个哈希表, 在哈希表中做dp
        Map<Integer, Integer> hash = new HashMap<Integer, Integer>(); //arr[i], dp[i]
        
        int ret = 1;
        for(int a : arr) {
            hash.put(a, hash.getOrDefault(a-difference, 0) + 1);
            ret = Math.max(ret, hash.get(a));
        }
        return ret;
    }
}